﻿
	

	IF NOT EXISTS (select * from sys.objects where name = 'trg_group_hierarchy_insert' and type  = 'TR')
BEGIN
	/* Ajouter triggers sur medianamik_Group pour la hierarchie */
	declare @query nvarchar(max)
	select @query = '
	CREATE TRIGGER [trg_group_hierarchy_insert] ON dbo.medianamik_Group FOR INSERT
			AS
			DECLARE @numrows AS int
			SET @numrows = @@rowcount
			IF @numrows > 1
			BEGIN
			  RAISERROR(''Only single row inserts are supported on medianamik_group!'', 16, 1)
			  ROLLBACK TRAN
			END
			ELSE
			IF @numrows = 1
			BEGIN
			  UPDATE E
			  SET depth =
					CASE
					  WHEN E.ParentGroupId IS NULL THEN 0
					  ELSE M.depth + 1
					END,
				  [path] =
					CASE
					  WHEN E.ParentGroupId IS NULL THEN ''.'' 
					  ELSE M.[path]
					END + CAST(E.GroupId AS varchar(50)) + ''.''
			  FROM medianamik_group AS E JOIN inserted AS I
				ON I.GroupId = E.GroupId
				LEFT OUTER JOIN medianamik_group AS M
				ON E.ParentGroupId = M.GroupId
			END


			
	' 
	EXEC sp_executesql @query
END

IF NOT EXISTS (select * from sys.objects where name = 'trg_group_hierarchy_update' and type  = 'TR')
BEGIN


	/* Ajouter triggers sur medianamik_Group pour la hierarchie */
	select @query = '

CREATE TRIGGER [trg_group_hierarchy_update] ON [dbo].[medianamik_group] FOR UPDATE
			AS
			IF UPDATE(groupId)
			BEGIN
			  RAISERROR(''Updates to groupId are not allowed on medianamik_group!'', 16, 1)
			  ROLLBACK TRAN
			END
			ELSE
			IF UPDATE(ParentgroupId)
			BEGIN
			  UPDATE E
			  SET depth = E.depth - I.depth + CASE
										  WHEN I.ParentgroupId IS NULL THEN 0
										  ELSE M.depth + 1
										END,
				  [path] = ISNULL(M.[path], ''.'') +
							  CAST(I.groupId AS varchar(50)) + ''.'' +
							  right(E.[path], len(E.[path]) - len(I.[path]))
			  FROM medianamik_group AS E JOIN INSERTED AS I
				ON E.[path] LIKE I.[path] + ''%''
				LEFT OUTER JOIN medianamik_group AS M
				ON I.ParentgroupId = M.groupid
			END
		
	' 
	EXEC sp_executesql @query
END

/* Mettre les groupes existants a jour (en hierarchie) */
IF NOT EXISTS(SELECT NULL FROM Medianamik_Group WHERE GroupId = '48849835-9843-43BC-B277-152D1D8775DE' AND parentgroupid = '48849835-9843-43bc-b277-152d1d8775de')  BEGIN update dbo.medianamik_Group set parentgroupid = '48849835-9843-43bc-b277-152d1d8775de' where groupid = '4f1298a0-61d4-4b72-badc-28094965b3d5' END
IF NOT EXISTS(SELECT NULL FROM Medianamik_Group WHERE GroupId = 'b88631f0-003a-4363-a758-3a29187e4e6c' AND parentgroupid = '7c726242-3979-4bb3-a742-5fcc16a6ba17')  BEGIN update dbo.medianamik_Group set parentgroupid = '7c726242-3979-4bb3-a742-5fcc16a6ba17' where groupid = 'b88631f0-003a-4363-a758-3a29187e4e6c' END
IF NOT EXISTS(SELECT NULL FROM Medianamik_Group WHERE GroupId = '7c726242-3979-4bb3-a742-5fcc16a6ba17' AND parentgroupid = '4f1298a0-61d4-4b72-badc-28094965b3d5')  BEGIN update dbo.medianamik_Group set parentgroupid = '4f1298a0-61d4-4b72-badc-28094965b3d5' where groupid = '7c726242-3979-4bb3-a742-5fcc16a6ba17' END
IF NOT EXISTS(SELECT NULL FROM Medianamik_Group WHERE GroupId = 'f843abf6-3030-4544-af2b-76f6d8d18de4' AND parentgroupid = 'b88631f0-003a-4363-a758-3a29187e4e6c')  BEGIN update dbo.medianamik_Group set parentgroupid = 'b88631f0-003a-4363-a758-3a29187e4e6c' where groupid = 'f843abf6-3030-4544-af2b-76f6d8d18de4' END
IF NOT EXISTS(SELECT NULL FROM Medianamik_Group WHERE GroupId = '8dea16a6-81dd-4d9c-be66-c48fb05dafef' AND parentgroupid = 'b88631f0-003a-4363-a758-3a29187e4e6c')  BEGIN update dbo.medianamik_Group set parentgroupid = 'b88631f0-003a-4363-a758-3a29187e4e6c' where groupid = '8dea16a6-81dd-4d9c-be66-c48fb05dafef' END
IF NOT EXISTS(SELECT NULL FROM Medianamik_Group WHERE GroupId = 'e5556bf7-a41d-43a8-99dd-da0ab9db3a7b' AND parentgroupid = '7c726242-3979-4bb3-a742-5fcc16a6ba17')  BEGIN update dbo.medianamik_Group set parentgroupid = '7c726242-3979-4bb3-a742-5fcc16a6ba17' where groupid = 'e5556bf7-a41d-43a8-99dd-da0ab9db3a7b' END


	update dbo.medianamik_Group set parentgroupid = 'b88631f0-003a-4363-a758-3a29187e4e6c' where parentgroupid is null and groupid <> '48849835-9843-43bc-b277-152d1d8775de'


	UPDATE dbo.medianamik_Group SET Path='.' + CAST(groupid AS varchar(36)) + '.', Depth=0 WHERE ParentGroupId Is Null

	WHILE EXISTS (SELECT * FROM dbo.medianamik_Group WHERE Depth Is Null) 
	UPDATE T SET T.depth = P.Depth + 1, 
	T.path = P.path + CAST(T.groupid AS varchar(36)) + '.'
	FROM dbo.medianamik_Group AS T 
	INNER JOIN dbo.medianamik_Group AS P ON (T.ParentgroupId=P.groupid) 
	WHERE P.Depth>=0 
	AND P.path Is Not Null 
	AND T.depth Is Null

DELETE FROM medianamik_Node
WHERE     (NodeId = '390e2bd2-cdbe-45ae-8e43-45062f5ae23e')

DELETE FROM medianamik_Node
WHERE     (NodeId = '65e6509d-09d8-4b73-b6e8-f5affd44f370')

DELETE FROM medianamik_Node
WHERE     (NodeId = '913bb12c-76c1-4b23-badc-ad74fa7599b4')

DELETE FROM medianamik_Node
WHERE     (NodeId = '453f2cfa-da50-4f86-9bdb-eeb27401a376')

DELETE FROM medianamik_Node
WHERE     (NodeId = 'b0e2f819-106b-434e-9818-3fc6b17db7b3')

update medianamik_NodePropertyValue set Value = '/modalwindows/ExportResourcesWindow.aspx' 
where NodeId = '45F7B8B3-95E8-46E4-9400-1635B9D4128F' AND PropertyId = '47096ACE-4C51-4C8F-BAE2-15A6FD90F6E9' AND CultureName = '' AND Revision IS NULL

